<?php
/**
 * @Name: NodeSearch.php
 * @Author: yashuai
 */

namespace App\Http\Search\Rbac;

use App\Http\Model\ConstCode;
use App\Http\Search\BaseSearch;
use App\Http\Model\Rbac\RoleModel;

class RoleSearch extends BaseSearch
{

    public function index()
    {
        $where       = [
            'RoleStatus' => _array_field($this->params, 'Status', ConstCode::BASIC_STATUS_NORMAL)
        ];
        $this->query = RoleModel::query()->with('node')->with('node.node')->where($where);
        $result      = $this->pageList();

        $childrenMap = [];
        if (!empty($result['list'])) {
            foreach ($result['list'] as $item) {
                foreach ($item['node'] as $info) {
                    if (is_array($info)) {
                        $childrenMap[$info['node']['ParentID']] = $info['node']['NodeID'];
                    }
                }
            }

            foreach ($result['list'] as &$val) {
                $nodeList = [];
                foreach ($val['node'] as $node) {
                    $nodeType = $node['node']['NodeType'];
                    if ($nodeType == ConstCode::NODE_TYPE_BUTTON && !isset($childrenMap[$node['NodeID']])) {
                        $nodeList[] = $node['NodeID'];
                    }
                }
                $val['NodeID'] = $nodeList;
                unset($val['node']);
            }
        }
        return $result;
    }

    /**
     * @Desc 获取角色ID关联
     * @author yashuai
     */
    public function roleMap()
    {
        $where                  = [
            'RoleStatus' => ConstCode::BASIC_STATUS_NORMAL
        ];
        $this->params['IsPage'] = 'no';
        $this->query            = RoleModel::query()->where($where)->select(['RoleName', 'RoleID']);
        return $this->pageList();
    }

}
